package com.szosen.jkzj.activity.mbgy;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.szosen.database.DBService;
import com.szosen.database.Visit_info;
import com.szosen.database.Visit_info2;
import com.szosen.database.base_info;
import com.szosen.jkzj.R;
import com.szosen.jkzj.activity.BaseActivity;
import com.szosen.jkzj.activity.HealthCheckList;
import com.szosen.util.BackGroundThread;
import com.szosen.util.JktjBitmapUtil;
import com.szosen.util.ThreeCloumAdpter;
import com.szosen.util.ThreeCloumAdpterMBGY;
import com.szosen.util.ToastTool;

public class GlycuresisTureList extends BaseActivity {
	private Context context;
	private ListView listview;
	private Button addbtn;
	private List<String[]> datas;
	public static ThreeCloumAdpterMBGY mAdapter;
	private TextView tittle_tv;
	// private LinearLayout linear;

	// 更新界面标志
	private static final int LOAD_CONTENT = 0x10;
	// 确认按钮
	private static final int FRESH = 0x11;
	// 保存成功
	private static final int SAVE_OK = 0x12;
	// 保存失败
	private static final int SAVE_ERROE = 0x13;
	// public static final String HANDLER = "Handler";

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case LOAD_CONTENT:
				List<String[]> content = (List<String[]>) msg.obj;
				clearAndAddTitile();
				datas.addAll(content);
				mAdapter.notifyDataSetChanged();
				break;
			case FRESH:
				break;
			case SAVE_OK:
				// switchLock();
				break;
			case SAVE_ERROE:
				// T.showShort(getActivity(), "保存失败");
				break;
			default:
				break;
			}
		}

	};

	public void finishActivity(View view) {
		onBackPressed();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.hypertensionturelist);
		super.setTitleContent("【糖尿病】");
		context = this;
		initView();
	}

	@Override
	@SuppressLint("ResourceAsColor")
	protected void onResume() {
		super.onResume();
		loadDataFromDB();
	}

	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case ThreeCloumAdpterMBGY.WHAT_FAILED:
				ToastTool.showShort(context, "一次只能打印4份数据。");
				break;
			default:
				break;
			}
		};
	};

	public void printAll(View view) {
		new BackGroundThread(new BackGroundThread.BackGroundTask() {

			@Override
			public void process() {
				LinkedHashMap<String, String> map = mAdapter.getMapData();
				if (map == null || map.size() <= 0) {
					runOnUiThread(new Runnable() {
						public void run() {
							ToastTool.showShort(context, "请先选择打印数据！");
						}
					});
				} else {
					Iterator iter = map.keySet().iterator();
					List<Visit_info2> datas = new ArrayList<Visit_info2>();
					while (iter.hasNext()) {
						String key = (String) iter.next();
						String time = map.get(key);
						Visit_info2 info = new Visit_info2(time);
						List<Visit_info2> lists = (List<Visit_info2>) DBService
								.queryByKey(info);
						if (lists != null && lists.size() > 0) {
							Visit_info2 v = lists.get(0);
							datas.add(v);
						}
					}
					JktjBitmapUtil.createTNBBitmap(context, datas);
				}
			}
		}).start();
	}

	private void initView() {
		datas = new ArrayList<String[]>();
		addbtn = (Button) findViewById(R.id.hypertension_btn_add);
		listview = (ListView) findViewById(R.id.hypertension_listview);
		View ll = LinearLayout.inflate(context, R.layout.mbgy_listview_head,
				null);
		listview.addHeaderView(ll);
		mAdapter = new ThreeCloumAdpterMBGY(context, datas, mHandler);
		listview.setAdapter(mAdapter);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// parent.getCount()==1;
				String[] line = (String[]) parent.getItemAtPosition(position);
				if (line == null) {
					return;
				}
				Intent intent = new Intent(context, GlycuresisTure.class);
				intent.putExtra("time", line[3]);
				startActivity(intent);
			}
		});
		listview.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(final AdapterView<?> parent,
					View view, final int position, long id) {

				AlertDialog.Builder builder = new AlertDialog.Builder(
						GlycuresisTureList.this);
				builder.setMessage("您确定要删除该记录？");
				builder.setPositiveButton("确定",
						new AlertDialog.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								String[] ss = (String[]) parent
										.getItemAtPosition(position);
								// 用时间为唯一标准删除这条记录
								String time = ss[3];
								Visit_info2 bean = new Visit_info2();
								bean.setTime(time);
								DBService.delete(bean);
								loadDataFromDB();
							}
						});
				builder.setNegativeButton("取消", null);
				builder.create().show();
				return true;
			}
		});
		addbtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(GlycuresisTureList.this,
						GlycuresisTure.class);
				intent.putExtra("flag", "new");
				startActivity(intent);
			}
		});
	}

	private void clearAndAddTitile() {
		datas.clear();
	}

	private void loadDataFromDB() {
		// 先清空集合
		datas.clear();
		Visit_info2 queryBean = new Visit_info2();
		queryBean.setIdCard(BaseActivity.getIdCard());
		List<Object> list = DBService.getAll(queryBean);
		for (int i = 0; i < list.size(); i++) {
			Visit_info2 visit_info = (Visit_info2) list.get(i);
			String idCard = visit_info.getIdCard();
			if (idCard.equals(BaseActivity.getIdCard())) {
				String time = visit_info.getTime();
				String date = visit_info.getVisitDate();
				String visitType = visit_info.getVisitWay();
				if ("1".equals(visitType)) {
					visitType = "门诊";
				} else if ("2".equals(visitType)) {
					visitType = "家庭";
				} else if ("3".equals(visitType)) {
					visitType = "电话";
				} else {
					visitType = "无";
				}
				datas.add(new String[] { time, date, visitType, time });
			}
		}
		mAdapter.notifyDataSetChanged();
	}

}
